//
//  TextViewController.m
//  IconMaster
//
//  Created by 为为 on 13-6-8.
//  Copyright (c) 2013年 为为. All rights reserved.
//

#import "TextViewController.h"
#import "TextNoteLabel.h"
#import "CFontPickerViewController.h"
#import "CColorPickerViewController.h"
#import "HRColorPickerView.h"

@interface TextViewController (){

 }

@end

@implementation TextViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
		controllerDict = [NSMutableDictionary dictionary];
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
	[textButton setTitle:NSLocalizedString(@"text", @"")];
	[fontButton setTitle:NSLocalizedString(@"font", @"")];
	[colorButton setTitle:NSLocalizedString(@"color", @"")];
	
	[self enableFollowingKeyboard];
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardFrameChanged:) name:UIKeyboardWillChangeFrameNotification object:nil];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)viewDidUnload {
	textButton = nil;
    fontButton = nil;
    colorButton = nil;
    [super viewDidUnload];
}

#pragma mark action

- (IBAction)textButtonPressed:(id)sender {
	if (self.textFieldPressedBlock) {
		self.textFieldPressedBlock(self);
	}
    
    [self enableFollowingKeyboard];
    
	TextNoteLabel* noteLabel = (TextNoteLabel*)[self.textNoteSprite getViewWithClass:[TextNoteLabel class]];
	if (noteLabel) {
		self.textNoteSprite.isEditting = true;
		[noteLabel showKeyboard];
	}
}

- (IBAction)fontButtonPressed:(id)sender {
	if (self.fontButtonPressedBlock) {
		self.fontButtonPressedBlock(self);
	}
    [self hideKeyboard];
	[self hideColorPicker];
	[self showFontSelector];
}

- (IBAction)colorButtonPressed:(id)sender {
	if (self.colorButtonPressedBlock) {
		self.colorButtonPressedBlock(self);
	}
    [self hideKeyboard];
	[self hideFontSelector];
	[self showColorPicker];
}

#pragma mark keyboard

- (void)setViewPosByKeyBoardFrame:(CGRect)keyFrame
{
    CGRect rect = [self.view.superview convertRect:keyFrame fromView:nil];
	CGRect frame = self.view.frame;
	frame.origin = CGPointMake(0, rect.origin.y-toolbar.frame.size.height);
	frame.size = CGSizeMake(frame.size.width, toolbar.frame.size.height+rect.size.height);
	self.view.frame = frame;
}

- (void)keyboardFrameChanged:(NSNotification*)notification
{
	if (![self followingKeyboard]) {
		return;
	}
	
    CGRect beginRect;
    CGRect endRect;
    double duration;
    NSUInteger option;
    
    [[notification.userInfo objectForKey:UIKeyboardFrameBeginUserInfoKey] getValue:&beginRect];
    [[notification.userInfo objectForKey:UIKeyboardFrameEndUserInfoKey] getValue:&endRect];
    duration = [[notification.userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey] doubleValue];
    option = [[notification.userInfo objectForKey:UIKeyboardAnimationCurveUserInfoKey] integerValue];
	
    [self setViewPosByKeyBoardFrame:beginRect];
    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationDuration:duration];
    [UIView setAnimationCurve:option];
    [UIView setAnimationBeginsFromCurrentState:YES];
    [self setViewPosByKeyBoardFrame:endRect];
    [UIView commitAnimations];
}

- (void)hideKeyboard
{
	[self disableFollowingKeyboard];
	TextNoteLabel* noteLabel = (TextNoteLabel*)[self.textNoteSprite getViewWithClass:[TextNoteLabel class]];
	if (noteLabel) {
		self.textNoteSprite.isEditting = true;
		[noteLabel hideKeyboard];
	}
}

- (void)enableFollowingKeyboard
{
	followingKeyboard = true;
}

- (void)disableFollowingKeyboard
{
	followingKeyboard = false;
}

- (BOOL)followingKeyboard
{
	return followingKeyboard;
}

#pragma mark font

- (void)showFontSelector
{
	CFontPickerViewController* fontController = [controllerDict objectForKey:@"fontPicker"];
	if (!fontController) {
		fontController = [[CFontPickerViewController alloc] init];
		[fontController loadDefaulFontArray];
		[controllerDict setObject:fontController forKey:@"fontPicker"];
		
		__block typeof(self) bself = self;
		fontController.rowSelectedBlock = ^(CFontPickerViewController* fpc, int index){
			[bself fontPicker:fpc rowSelected:index];
		};
	}
	
	[self.view addSubview:fontController.view];
	fontController.view.frame = contentView.frame;
}

- (void)hideFontSelector
{
	CFontPickerViewController* fontController = [controllerDict objectForKey:@"fontPicker"];
	if (fontController) {
		[fontController.view removeFromSuperview];
	}
}

- (void)fontPicker:(CFontPickerViewController*)fontPicker rowSelected:(int)index
{
	UIFont* curFont = [self getTextNoteSpriteFont];
	if (curFont) {
		UIFont* font = [UIFont fontWithName:[fontPicker.fontArray objectAtIndex:index] size:curFont.pointSize];
		[self setTextNoteSpriteFont:font];
	}
}

- (void)setTextNoteSpriteFont:(UIFont*)font
{
	if (self.textNoteSprite) {
		TextNoteLabel* label = (TextNoteLabel*)[self.textNoteSprite getViewWithClass:[TextNoteLabel class]];
		if (label) {
			label.font = font;
			[label resizeSelf];
		}
	}
}

- (UIFont*)getTextNoteSpriteFont
{
	UIFont* font = nil;
	if (self.textNoteSprite) {
		UILabel* label = (UILabel*)[self.textNoteSprite getViewWithClass:[UILabel class]];
		if (label) {
			font = label.font;
		}
	}
	return font;
}

#pragma mark color

- (void)showColorPicker
{
    CColorPickerViewController* colorController = [controllerDict objectForKey:@"colorPicker"];
    if (!colorController) {
        colorController = [[CColorPickerViewController alloc] init];
        [controllerDict setObject:colorController forKey:@"colorPicker"];
        
        __block typeof(self) bself = self;
        colorController.colorChangedBlock = ^(CColorPickerViewController* ctl, UIColor* color) {
            [bself colorPicker:ctl colorSelected:color];
        };
    }
    
    [self.view addSubview:colorController.view];
    colorController.view.frame = contentView.frame;}

- (void)hideColorPicker
{
    CColorPickerViewController* colorController = [controllerDict objectForKey:@"colorPicker"];
    if (colorController) {
        [colorController.view removeFromSuperview];
    }
    
}

- (void)colorPicker:(CColorPickerViewController*)colorController colorSelected:(UIColor*)color
{
	[self setTextNoteSpriteColor:color];
}

- (UIColor*)getTextNoteSpriteColor
{
	UIColor* color = nil;
	if (self.textNoteSprite) {
		UILabel* label = (UILabel*)[self.textNoteSprite getViewWithClass:[UILabel class]];
		if (label) {
			color = label.textColor;
		}
	}
	return color;
}

- (void)setTextNoteSpriteColor:(UIColor*)color
{
	if (self.textNoteSprite) {
		UILabel* label = (UILabel*)[self.textNoteSprite getViewWithClass:[UILabel class]];
		if (label) {
			label.textColor = color;
		}
	}
}

@end
